package com.example.musicapi.service;

import com.example.musicapi.dao.RemarkDao;
import com.example.musicapi.entity.Remark;
import com.example.musicapi.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RemarkService {
    @Autowired
    private RemarkDao remarkDao;

    public Remark add(Remark s){
        return remarkDao.save(s);
    }
    public void delete(String id){
        remarkDao.deleteById(id);
    }

    public List<Remark> findAll(int page, int size){
        Pageable pageable = PageRequest.of(page, size);
        Page<Remark> userPage = remarkDao.findAll(pageable);
        return userPage.getContent();
    }
    public List<Remark> searchTxt(int page, int size,String key){
        Pageable pageable = PageRequest.of(page, size);
        Page<Remark> userPage = remarkDao.findBySearchTxt(key,pageable);
        return userPage.getContent();
    }
    public List<Remark> search(int page, int size,String key){
        Pageable pageable = PageRequest.of(page, size);
        Page<Remark> userPage = remarkDao.findBySearch(key,pageable);
        return userPage.getContent();
    }

    public Remark findById(String id) {
        return remarkDao.findDataById(id);
    }

    public void update(String id, String like_num){
        Remark d=remarkDao.findDataById(id );
        if (d!=null){
            d.setLike_num(like_num);
            remarkDao.save(d);
            return;
        }
        throw new RuntimeException("当前的:"+id+"信息中不存在");
    }
}

